package com.example.sqlitetest

import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.contentValuesOf
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlin.properties.Delegates

class MainActivity : AppCompatActivity() {

    companion object{
        const val TAG = "MainActivity"
    }
    lateinit var button: Button
    lateinit var addData_btn: Button
    lateinit var deleteData_btn: Button
    lateinit var updataData_btn: Button
    lateinit var queryData_btn: Button

    var bookId: String?=null


    @SuppressLint("Range")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()

        val dbHelper = MyDatabaseHelper(this, "BookStore.db",1)
        button.setOnClickListener {
            dbHelper.writableDatabase
        }

        addData_btn.setOnClickListener {
            val uri = ("content://com.example.sqlitetest.provider/book").toUri()
            val values = contentValuesOf("name" to "A Clash of Kings",
                "author" to "George Martin", "pages" to 1040, "price" to 22.85)
            val newUri = contentResolver.insert(uri,values)
            bookId = newUri?.pathSegments?.get(1)
        }
        queryData_btn.setOnClickListener {
            val uri = ("content://com.example.sqlitetest.provider/book").toUri()
            contentResolver.query(uri,null,null,null,null)?.apply {
                while (moveToNext()){
                    val name = getString(getColumnIndex("name"))
                    val author = getString(getColumnIndex("author"))
                    val pages = getString(getColumnIndex("pages"))
                    val price = getString(getColumnIndex("price"))
                    Log.d(TAG, "book name is $name")
                    Log.d(TAG, "book author is $author")
                    Log.d(TAG, "book pages is $pages")
                    Log.d(TAG, "book price is $price")

                }
                close()
            }
        }
        updataData_btn.setOnClickListener {
            bookId?.let {
                val uri = ("content://com.example.sqlitetest.provider/book/$it").toUri()
                val values = contentValuesOf("name" to "A Storm of Swords", "pages" to 1216, "price" to 24.09)
                contentResolver.update(uri,values,null,null)
            }
        }
        deleteData_btn.setOnClickListener {
            bookId?.let{
                val uri = ("content://com.example.sqlitetest.provider/book/$it").toUri()
                contentResolver.delete(uri, null, null)
            }
        }
    }

    fun init() {
        button = findViewById(R.id.createDatabase)
        addData_btn = findViewById(R.id.addData)
        deleteData_btn = findViewById(R.id.deleteData)
        updataData_btn = findViewById(R.id.updateData)
        queryData_btn = findViewById(R.id.queryData)

    }
}